<?php
namespace Web\Controller;
use Think\Controller;
use Web\Logic\RoleLogic;
use Web\Logic\CommonLogic;
class SetAuthorityController extends BaseController {

    //角色
	public function index(){
            //实例化 logic
        $id = $_REQUEST['id'];
        $rolename = $_REQUEST['name'];
        $type =  $_REQUEST['t'];
        $data = $_REQUEST['data'];
        $sub = $_REQUEST['sub'];
        if($sub){
            $this->SetAuthorityForDevice($data,$id,$type);
            if($type == 1){
                $url = U('role/index');
            } else{
                $url =  U('member/index');
            }
            $this ->success('设置成功',$url);
            exit;
        }
        else
        {

            $rolelist = $this->getOneSql($id,$type);
            $device = $this->getDevices($id,$type);
            $this->assign("type",$type);
            $this->assign("info",$rolelist);
            $this->assign("device_class",$device);
            $this->display();
        }

	}

    function getOneSql($id,$type){
        if($type == 1){
             //实例化模块
            $mod = M("role");
            return $mod->where("roleid = '$id'")
                    ->field('roleid as id,rolename as name')
                    ->find();;
        } else{
              //实例化模块
            $mod = M("member");
            return $mod->where("userid = '$id'")
                    ->field('userid as id  ,username as name')
                    ->find();;
        }

    }

    /**
    * put your comment there...
    *
    * @param mixed $data
    * @param mixed $id
    * @param mixed $type  1:给角色赋 2：给用户赋
    */
    function SetAuthorityForDevice($data,$id,$type){
         set_time_limit(0);
         if($type == 1){
             $itemName = "role_id";
             $table_name ="role_control_auth";
             $table_param_name ="role_param_control_auth";
         }else{
             $itemName = "user_id";
             $table_name ="device_control_auth";
             $table_param_name ="device_param_control_auth";
         }
           $temp = array();
           $arrayList = array();
           foreach($data as $k =>$v){

                if($v =="on"){
                    $array_list = explode("_",$k);
                    $class_id = $array_list[0];
                    $cpn_id = $array_list[1];
                    $nums = $array_list[2];
                    $array_k = $class_id ."_".$cpn_id;
                    $value = $v ;
                    $temp[$array_k][$itemName] = $id;
                    $temp[$array_k]['class_id'] = $class_id;
                    $temp[$array_k]['cpn_id'] = $cpn_id;
                    if(is_numeric($nums)){
                        if($arrayList[$array_k]['control_status']){
                            $temp[$array_k]['control_status'] = max($arrayList[$array_k]['control_status'],$nums);
                        }else{
                            $temp[$array_k]['control_status'] = $nums;
                        }
                    }
                 }
            }

            $where[$itemName] = $id;
            $mod = M($table_name);
            $mod->where($where)->delete();
            $arrayList = array_values($temp);

            $mod->addAll($arrayList);

            //取得所有设置的参数 ，将参数信息也设置进去
            $param_list = array();
            foreach($arrayList as $k =>$v){

                $list = $v;

                //exit;
                //取得该device的所有参数
                $parmlist = $this->getDeviceParam($list['cpn_id']);

                foreach($parmlist as $mk =>$mv){
                    $temp=array();
                    $temp[$itemName] =  $list[$itemName];
                    $temp['class_id'] = $list['class_id'];
                    $temp['cpn_id'] = $list['cpn_id'];
                    $temp['parm_id'] = $mv['parm_id'];
                    $temp['control_status'] = $list['control_status'];
                    $param_list[] =$temp;
                }

            }
            $mod = M($table_param_name);
            $mod->where($where)->delete();
            $param_list = array_values($param_list);
            $mod->addAll($param_list);

             //如果是角色则需要更新此角色下的所有用户
             /**
            if($type ==1){
                //取得这个角色下的所有用户
                $member_where['roleid']= $id;
                $member_list = M('member')->where($member_where)->select();
                $mm =  new MemberController();
                foreach($member_list as $k =>$v){
                     $mm->updateRole($id,$v['userid']);
                }

            }**/

    }

    public function getDeviceParam($id){
         $mod = M('cpn');
         $where['d.id'] = $id;
         $list = $mod->alias("d")
         ->join(C('DB_PREFIX')."devices_param_relation d_r on d.id = d_r.cpn_id" )
         ->join(C('DB_PREFIX')."devices_class d_c on d.cpn_type = d_c.class_id" )
         ->join(C('DB_PREFIX')."devices_class_parm d_c_p on d_c_p.device_class = d.cpn_type and d_c_p.id = d_r.param_id" )
         ->field("d.id,d.cpn_type,d_c_p.id as parm_id,d_c_p.parm_name")
         ->where($where)
         ->select();
         return $list;
  }




    public function detail($cpn_id,$roleid,$class_id,$type){

        //实例化 logic
        $device = $this->getDevicesList($cpn_id,$roleid,$class_id,$type);
        $this->assign("cpn_id",$cpn_id);
        $this->assign("id",$roleid);
        $this->assign("class_id",$class_id);
        $this->assign("device_list",$device);
        $this->assign("type",$type);
        $this->display();

    }

    public function setDetail(){
        $id = $_REQUEST['id'];
        $cpn_id = $_REQUEST['cpn_id'];
        $class_id = $_REQUEST['class_id'];
        $type = $_REQUEST['type'];
        $data = $_REQUEST['data'];
         if($type == 1){
             $itemName = "role_id";
             $table_name ="role_control_auth";
             $table_param_name ="role_param_control_auth";
         }else{
             $itemName = "user_id";
             $table_name ="device_control_auth";
             $table_param_name ="device_param_control_auth";
         }
         $temp = array();
        foreach($data as $k =>$v){

            if($v =="on"){
                $array_list = explode("_",$k);
                $parm_id = $array_list[0];
                $nums = $array_list[1];
                $array_k = $parm_id;
                $value = $v ;
                $temp[$array_k][$itemName] = $id;
                $temp[$array_k]['parm_id'] = $parm_id;
                $temp[$array_k]['class_id'] = $class_id;
                 $temp[$array_k]['cpn_id'] = $cpn_id;
                if(is_numeric($nums)){
                    if($arrayList[$array_k]['control_status']){
                        $temp[$array_k]['control_status'] = max($arrayList[$array_k]['control_status'],$nums);
                    }else{
                        $temp[$array_k]['control_status'] = $nums;
                    }
                }
             }
        }
        dump($temp);
        $mod = M($table_param_name);
        $where[$itemName] = $id;
        $where['cpn_id'] = $cpn_id;

        $mod->where($where)->delete();


        $arrayList = array_values($temp);
        $mod->addAll($arrayList);


    }





    function getDevices($id,$type){
        $mod = M('devices_class');
        $returnValue = array();
        if($type == 1){
            $list = $mod->alias("d_c")
              ->join(C('DB_PREFIX')."cpn d on d.cpn_type = d_c.class_id" )
              ->join("left join ".C('DB_PREFIX')."role_control_auth  ra on ra.cpn_id = d.id  and ra.role_id = $id" )
              ->field("d_c.*,d.id as cpn_id,d.cpn_name,d.show_name,ra.control_status")
              ->where("d_c.is_show=1")->select();
        }else{
             $list = $mod->alias("d_c")
              ->join(C('DB_PREFIX')."cpn d on d.cpn_type = d_c.class_id" )
              ->join("inner join ".C('DB_PREFIX')."device_control_auth  ra on ra.cpn_id = d.id  and ra.user_id = $id" )
              ->field("d_c.*,d.id as cpn_id,d.cpn_name,d.show_name,ra.control_status")
              ->where("d_c.is_show=1")->select();
             // echo $mod->getLastSql();

        }
        for($i =0; $i <count($list);$i++){
             $class_name =  $list[$i]['class_name'];
             $returnValue[$class_name][] = $list[$i];
        }
        //print_r($returnValue);
        return $returnValue;
    }

    function getDevicesList($cpn_id,$roleid,$class_id,$type){
        $mod = M('devices_class_parm');
        if($type == 1){
            $where['dcp.device_class'] = $class_id;
            $list = $mod
            ->alias("dcp")
            ->field("dcp.*,rpca.control_status")
            ->join(C('DB_PREFIX')."devices_param_relation  dpr on dcp.id = dpr.param_id  and dpr.cpn_id = $cpn_id" )
            ->join("left join ".C('DB_PREFIX')."role_param_control_auth  rpca on dcp.id = rpca.parm_id  and rpca.role_id = $roleid and rpca.cpn_id = $cpn_id" )
            ->where($where)->select();
        }else{
            $where['dcp.device_class'] = $class_id;
            $list = $mod
            ->alias("dcp")
            ->field("dcp.*,rpca.control_status")
            ->join(C('DB_PREFIX')."devices_param_relation  dpr on dcp.id = dpr.param_id  and dpr.cpn_id = $cpn_id" )
            ->join("left join ".C('DB_PREFIX')."device_param_control_auth  rpca on dcp.id = rpca.parm_id  and rpca.user_id = $roleid and rpca.cpn_id = $cpn_id" )
            ->where($where)->select();
        }
        return $list;
    }

}